package com.augurit.gzcgwsp.lhsp.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.xwork.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;

import com.augurit.common.util.PageUtils;
import com.augurit.gzcgwsp.lhsp.convert.ProcessInstanceConvert;
import com.augurit.gzcgwsp.lhsp.dao.LhspSendItemNoticeDao;
import com.augurit.gzcgwsp.lhsp.entity.LhspSendItemNotice;
import com.augurit.gzcgwsp.lhsp.service.IProcessInstanceService;
import com.augurit.gzcgwsp.lhsp.service.ILhspUpdateFlowStateService;
import com.augurit.gzcgwsp.lhsp.web.form.LhspSendItemNoticeForm;
import com.augurit.gzcgwsp.wfex.common.service.IWfServiceEX;
import com.augurit.wf.common.service.impl.WfBusClientServiceImpl;

/**
 * 异构系统提供接口  消息通知接口
 * @author adm
 *
 */
@Service
@Transactional
public class ProcessInstanceServiceImpl extends WfBusClientServiceImpl<LhspSendItemNoticeForm>  implements IProcessInstanceService {
	private static final Logger log=Logger.getLogger(ProcessInstanceServiceImpl.class);
	@Autowired
	private LhspSendItemNoticeDao lhspSendItemNoticeDao;
	@Autowired
	private ILhspUpdateFlowStateService lhspSendCommandNoticeService;
	@Autowired
	private IWfServiceEX wfServiceEX;
	
	/**
	 * 提供给政务中心调用的类，把案件信息传过来，保存
	 * @param projectId 工程编号
	 * @param ywName 业务名称
	 * @return 正常返回string值，接收成功的返回"成功"，其他返回"失败"
	 */
	public String SendItemNotice(String projectId,String ywName) {
		try {
			if(StringUtils.isBlank(projectId) || StringUtils.isBlank(ywName)){
				return "失败";
			}
			String[] ywNameArr = ywName.split(";");
			for(int i = 0;i<ywNameArr.length;i++){
				LhspSendItemNotice sendItemNotice = new LhspSendItemNotice();
				sendItemNotice.setProjectId(projectId);
				if(ywNameArr[i].contains("临时占用审批")){
					sendItemNotice.setTemplateCode("csdllszysp");
				}else if(ywNameArr[i].contains("挖掘审批")){
					sendItemNotice.setTemplateCode("csdlwj");
				}else if(ywNameArr[i].contains("房屋建筑工程类")){
					sendItemNotice.setTemplateCode("jzfqwczzpf_xz");
				}else if(ywNameArr[i].contains("房屋拆卸类")){
					sendItemNotice.setTemplateCode("jzfqwczzpf_xz");
				}else if(ywNameArr[i].contains("平整场地(含劈山)工程类")){
					sendItemNotice.setTemplateCode("jzfqwczzpf_xz");
				}else if(ywNameArr[i].contains("市政建设工程(包括道路、桥梁、管线、地铁工程)类")){
					sendItemNotice.setTemplateCode("jzfqwczzpf_xz");
				}else if(ywNameArr[i].contains("重点项目报批绿色通道工程类")){
					sendItemNotice.setTemplateCode("jzfqwczzpf_xz");
				}else if(ywNameArr[i].contains("减免建筑垃圾处置费工程类")){
					sendItemNotice.setTemplateCode("jzfqwczzpf_xz");
				}else if(ywNameArr[i].contains("消纳建筑废弃物类")){
					sendItemNotice.setTemplateCode("jzfqwczzxn_xz");
				}else if(ywNameArr[i].contains("建筑废弃物处置证延期")){
					sendItemNotice.setTemplateCode("jzfqwczzpf_yq");//TODO 这个无法确定是排放还是消纳的延期   目前先默认是  ：排放延期
				}else{
					return "失败";
				}
				sendItemNotice.setItemName(ywNameArr[i]);
				sendItemNotice.setIsLhsp("Y");//初始值设置为Y，如果收了这个案，则把它设置为N
				this.save(sendItemNotice);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return "失败";
		}
		return "成功";//
	}
	
//	/**
//	 * 联合消息
//	 * 并联审批系统向异构系统发送敦促等日常要求的通知，如勘探、会审、验收等通知。
//	 * @param acceptSerialNum 统一办理编号
//	 * @param content 消息内容
//	 * @param sendId 发送人ID
//	 * @param sendName 发送人
//	 * @param receive 接收单位（字典）
//	 * @return 正常返回string值，办理完成的返回1，办理中返回0,系统异常返回F+失败原因，如：F当前用户名为空。
//	 */
//	public String sendCommandNotice(String acceptSerialNum,String content,String sendId,String sendName,String receive){
//		try {
//			lhspSendCommandNoticeService.sendCommandNotice(acceptSerialNum, content, sendId, sendName, receive);
//		} catch (Exception e) {			
//			e.printStackTrace();
//			return "F"+e.getMessage();
//		}
//		return "";//TODO 办理完成的返回1，办理中返回0
//	}
	
	/**
	 * 修改isLhsp状态
	 * @param id
	 * @return
	 */
	public String changeIsLhsp(String id){
		StringBuffer sql = new StringBuffer();
    	sql.append("update LhspSendItemNotice t set t.isLhsp='N'");
    	sql.append(" where t.id="+id);
    	lhspSendItemNoticeDao.getSession().createQuery(sql.toString()).executeUpdate();
    	System.out.println("改变isLhsp状态为：N");
		return null;
	}
	
	@Override
	public Page<LhspSendItemNoticeForm> search(Page<LhspSendItemNoticeForm> page,
			List<PropertyFilter> filters) {
		// TODO Auto-generated method stub
		return null;
	}
	/**
	 * 查询
	 * @param page 
	 * @param form 
	 * @return page
	 */
	public Page<LhspSendItemNoticeForm> search(Page<LhspSendItemNoticeForm> page,
			LhspSendItemNoticeForm form) {
		Page pg = PageUtils.newPageInstance(page);
		StringBuffer hql = new StringBuffer("from LhspSendItemNotice t where 1=1 and t.isLhsp='Y'");
		List values = new ArrayList();
		lhspSendItemNoticeDao.findPage(pg, hql.toString(), values);		
		List<LhspSendItemNoticeForm> list = ProcessInstanceConvert.convertVoListToFormList(pg.getResult());
		PageUtils.copy(pg, list, page);
		return page;
	}
	
	/**
	 * 修改答复时间，收案时间加2个工作日
	 * @param startDate
	 * @param procInstId
	 */
	public void updateDfsjDate(Date startDate ,String procInstId){
		String procInstId2 = procInstId.split("\\.")[1];
		Date newdate=wfServiceEX.calculateDueDate(startDate,2);
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dutedate = sf.format(newdate);
    	System.out.println("dutedate = "+dutedate);
    	String s = "to_date('"+dutedate+"','yyyy-mm-dd hh24:mi:ss')";
    	String sql = "update jbpm4_hist_procinst t set t.process_duedate="+s+" where t.dbid_='"+procInstId2+"'";
    	lhspSendItemNoticeDao.getSession().createSQLQuery(sql.toString()).executeUpdate();
	}
	@Override
	public void delete(Long... ids) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public LhspSendItemNoticeForm get(Long id) {
		LhspSendItemNotice entity = lhspSendItemNoticeDao.get(id);
		LhspSendItemNoticeForm form = ProcessInstanceConvert.convertVoToForm(entity);
		return form;
	}
	@Override
	public void save(LhspSendItemNoticeForm... forms) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void save(LhspSendItemNoticeForm forms) {
		// TODO Auto-generated method stub
		
	}
	public void save(LhspSendItemNotice lhspSendItemNotice){
		lhspSendItemNoticeDao.save(lhspSendItemNotice);
	}

}
